package org.jeecg.modules.jxc.sale.service.impl;

import org.jeecg.modules.jxc.sale.entity.SaleListGoods;
import org.jeecg.modules.jxc.sale.entity.SaleList;
import org.jeecg.modules.jxc.sale.mapper.SaleListMapper;
import org.jeecg.modules.jxc.sale.mapper.SaleListGoodsMapper;
import org.jeecg.modules.jxc.sale.service.ISaleListGoodsService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: tb_sale_list_goods
 * @Author: jeecg-boot
 * @Date:   2021-10-26
 * @Version: V1.0
 */
@Service
public class SaleListGoodsServiceImpl extends ServiceImpl<SaleListGoodsMapper, SaleListGoods> implements ISaleListGoodsService {

	@Autowired
	private SaleListGoodsMapper saleListGoodsMapper;
	@Autowired
	private SaleListMapper saleListMapper;

	@Override
	@Transactional
	public void saveMain(SaleListGoods saleListGoods, List<SaleList> saleListList) {
		saleListGoodsMapper.insert(saleListGoods);
		if(saleListList!=null && saleListList.size()>0) {
			for(SaleList entity:saleListList) {
				//外键设置
				entity.setId(saleListGoods.getId());
				saleListMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(SaleListGoods saleListGoods,List<SaleList> saleListList) {
		saleListGoodsMapper.updateById(saleListGoods);

		//1.先删除子表数据
		saleListMapper.deleteByMainId(saleListGoods.getId().toString());

		//2.子表数据重新插入
		if(saleListList!=null && saleListList.size()>0) {
			for(SaleList entity:saleListList) {
				//外键设置
				entity.setId(saleListGoods.getId());
				int insert = saleListMapper.insert(entity);
				System.out.println(insert);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		saleListMapper.deleteByMainId(id);
		saleListGoodsMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			saleListMapper.deleteByMainId(id.toString());
			saleListGoodsMapper.deleteById(id);
		}
	}
	@Override
	public List<SaleListGoods> selectByMainId(String mainId) {
		return saleListGoodsMapper.selectByMainId(mainId);
	}

}
